package com.day0916.task;

import java.util.Arrays;
import java.util.Scanner;

public class OrdersManager {
    Scanner in = new Scanner(System.in);

    DishManager dishManager;
    Orders[] orders = new Orders[2];

    public void initOrders(){
        Orders orders1 = new Orders();
        orders1.user_names = "张清";
        orders1.times = 12;
        orders1.addresses = "天成路207号";
        orders1.sum_money = 50;
        orders1.states = 1;
        orders1.dish_mes = "红烧带鱼 2份";
        orders[0] = orders1;

        Orders orders2 = new Orders();
        orders2.user_names = "张清";
        orders2.times = 18;
        orders2.addresses = "天成路207号";
        orders2.sum_money = 37.2;
        orders2.states = 0;
        orders2.dish_mes = "鱼香肉丝 2份";
        orders[1] = orders2;

    }

    //   2   显示餐袋
    public void viewMealBags(){
        for(int i = 0;i < orders.length; i ++){
                String state = (orders[i].states == 1) ? "已完成" : "已预定";
                System.out.println(i+1 + "\t\t" + orders[i].user_names + "\t" + orders[i].dish_mes + "\t" + orders[i].times + "\t\t\t" + orders[i].addresses + "\t\t\t" + orders[i].sum_money + "\t" + state);
        }
    }

    //   1   添加订单
    public void addOrder(String user_name,int time,String address,double sum,int dish_no,int dish_quantity){
        //添加到订单中
        orders = Arrays.copyOf(orders,orders.length + 1);
        Orders order = new Orders();
        order.user_names = user_name;
        order.times = time;
        order.addresses = address;
        order.sum_money = sum;
        order.dish_mes = dishManager.dishes[dish_no-1].dishName + " " + dish_quantity + "份";
        orders[orders.length - 1] = order;
    }

    //   3   签收订单
    public void signInOrders(int order_no){
        orders[order_no - 1].states = 1;
        System.out.println("订单已签收!");
    }

    //   5   删除订单
    public void deleteOrders(int del_order){
        if(orders[del_order - 1].states == 1){
            for(int i = del_order - 1; i < orders.length - 1; i ++){
                orders[i].user_names = orders[i+1].user_names;
                orders[i].dish_mes = orders[i+1].dish_mes;
                orders[i].times = orders[i+1].times;
                orders[i].addresses = orders[i+1].addresses;
                orders[i].sum_money = orders[i+1].sum_money;
                orders[i].states = orders[i+1].states;
            }
            orders = Arrays.copyOf(orders,orders.length-1);
            System.out.println("订单删除成功！");
        }
        else {
            System.out.println("您选择的订单未完成，不可删除！");
        }
    }

    //   4   修改订单
    public void change_orders(int orderN0){
        boolean flag = false;
        for (int i = 0; i < orders.length; i ++){
            if ((orderN0 == (i+1)) && (orders[i].states == 0)){
                flag = true;
            }
        }
        if (flag == true){
            System.out.println("请输入要修改的信息（1.订餐人，2.餐品信息，3.送餐时间，4.送餐地址）：");
            int choose = in.nextInt();
            switch (choose){
                case 1:
                    System.out.print("请输入修改后的订餐人：");
                    String u_name = in.next();
                    System.out.println("修改成功，修改后的信息如下：");
                    for (int i = 0; i < orders.length; i ++){
                        if (orderN0 == (i+1)){
                            orders[i].user_names = u_name;
                            String state = (orders[i].states == 1) ? "已完成" : "已预定";
                            System.out.println(i+1 + "\t\t" + orders[i].user_names + "\t" + orders[i].dish_mes + "\t" + orders[i].times + "\t\t\t" + orders[i].addresses + "\t\t\t" + orders[i].sum_money + "\t" + state);
                        }
                    }
                    break;
                case 2:
                    dishManager.showDish();
                    System.out.print("请选择修改后的餐品序号：");
                    int dishNo = in.nextInt();
                    for (int j = 0; j < dishManager.dishes.length; j ++){
                        if (dishNo == dishManager.dishes[j].dishNo){
                            System.out.print("请输入修改后的数量：");
                            int dish_num = in.nextInt();
                            orders[j].dish_mes = orders[j].dish_mes + " " + dish_num + "份";
                            orders[j].sum_money = dishManager.dishes[j].dishPrice * dish_num;
                        }
                    }
                    System.out.println("修改成功，修改后的信息如下：");
                    for (int i = 0; i < orders.length; i ++){
                        if (orderN0 == (i+1)){
                            String state = (orders[i].states == 1) ? "已完成" : "已预定";
                            System.out.println(i+1 + "\t\t" + orders[i].user_names + "\t" + orders[i].dish_mes + "\t" + orders[i].times + "\t\t\t" + orders[i].addresses + "\t\t\t" + orders[i].sum_money + "\t" + state);
                        }
                    }
                    break;
                case 3:
                    System.out.print("请输入修改后的送餐时间：");
                    int dish_time = in.nextInt();
                    System.out.println("修改成功，修改后的信息如下：");
                    for (int i = 0; i < orders.length; i ++){
                        if (orderN0 == (i+1)){
                            orders[i].times = dish_time;
                            String state = (orders[i].states == 1) ? "已完成" : "已预定";
                            System.out.println(i+1 + "\t\t" + orders[i].user_names + "\t" + orders[i].dish_mes + "\t" + orders[i].times + "\t\t\t" + orders[i].addresses + "\t\t\t" + orders[i].sum_money + "\t" + state);
                        }
                    }
                    break;
                case 4:
                    System.out.print("请输入修改后的送餐地址：");
                    String address = in.next();
                    System.out.println("修改成功，修改后的信息如下：");
                    for (int i = 0; i < orders.length; i ++){
                        if (orderN0 == (i+1)){
                            orders[i].addresses = address;
                            String state = (orders[i].states == 1) ? "已完成" : "已预定";
                            System.out.println(i+1 + "\t\t" + orders[i].user_names + "\t" + orders[i].dish_mes + "\t" + orders[i].times + "\t\t\t" + orders[i].addresses + "\t\t\t" + orders[i].sum_money + "\t" + state);
                        }
                    }
                    break;
                default:
                    System.out.println("输入错误，请重新输入（1-4）：");
                    choose = in.nextInt();
                    break;
            }
        }
        else {
            System.out.println("抱歉，没有这个订单或订单状态已完成！");
        }
    }
}
